package com.ncr.teradata.bireport;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.telecom.cms.dto.CampCustAdvInfoEx;

/**
 * ReportCache.java
 * 报表缓存类，在这里缓存了系统中所有的报表（指ReportData对象）
 *
 * @author ylpeng
 * @date Mar 12, 2007
 */
public class ReportCache
{
	//报表集合，系统中所有的报表都存放在该Map中，该对象为static，所有Report对象共享同一个ReportData中的数据
	private static Map reports = new HashMap();
	
	private static ReportCache instance = new ReportCache();
	private ReportCache(){}
	public static ReportCache getInstance(){
		return instance;
	}
	
	/**
	 * 得到缓存中的报表数据
	 *
	 *@param reportName String 报表名称
	 *@return ReportData 返回指定名称的报表数据
	 */
	public static ReportData getCachedReportData(String reportName){
		if (null!=reports.get(reportName.toUpperCase()))
		    return (ReportData)reports.get(reportName.toUpperCase());
		
        //use file to read cache info   
		ReportData p=null;
    	ObjectInputStream ois=null;
		try {
			 ois = new ObjectInputStream(
			           new FileInputStream("report_"+reportName+".txt"));
			 if((p=(ReportData)ois.readObject())!=null)
			 {
				 ois.close();	 
				 return p;
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			try {
				if(ois!=null) ois.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 设置缓存中报表数据
	 *
	 *@param reportName String 报表名称
	 *@param data ReportData 报表数据
	 */
	public static void setCachedReportData(String reportName,ReportData data){
		//reports.put(reportName.toUpperCase(),data);
		
		ObjectOutputStream oos=null;
		try {
			oos = new ObjectOutputStream(
			            new FileOutputStream("report_"+reportName+".txt"));
			oos.writeObject(data);
			oos.writeObject(null);//write a null object mark the end of file
			oos.close();	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			try {
				if(oos!=null)oos.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
}
